<template>
  <div class="sysDepartment">

    <div class="searchArea">
      <span style="font-size: 13px"> 内容：</span>
      <el-input
        class="nameInput"
        placeholder="请输入内容"
        size="small">
      </el-input>
      <el-button type="primary" size="small" icon="el-icon-search" @click="loadData">搜索</el-button>
      <el-button type="primary" size="small" icon="el-icon-refresh-right" @click="reset">重置</el-button>
    </div>

    <div class="actionArea">
      <el-button type="primary" size="small" icon="el-icon-plus" @click="openAddDialog">添加</el-button>
    </div>

    <div class="dataArea">
      <el-table
        :header-cell-style="{'text-align':'center','height':'49px','color':'black','background-color':'rgb(250,250,250)'}" m
        :cell-style="{'text-align':'center'}"
        border
        row-key="id"
        size="small"
        :tree-props="{children: 'children', hasChildren:'hasChildren' }"
        :data="sysDepartmentList"
      >
        <el-table-column
          prop="createTime"
          label="创建时间"
        >
        </el-table-column>
        <el-table-column
          prop="departName"
          label="部门名称"
        >
        </el-table-column>

        <el-table-column
          prop="status"
          label="状态"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.status===1">正常</span>
            <span v-if="scope.row.status===2">已冻结</span>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
        >
          <template slot-scope="scope">
            <el-button type="text" size="small" style="margin-right: 10px" @click="openEditDialog(scope.row)">编辑</el-button>
            <el-popconfirm
              title="确定要删除该记录吗？"
              icon="el-icon-delete"
              @onConfirm="confirmDeleteSysDepartment(scope.row.id)"
            >
              <el-button type="text" slot="reference" style="color: red" size="small">删除</el-button>
            </el-popconfirm>
          </template>

        </el-table-column>
      </el-table>

      <div class="page">
        <el-pagination
          background
          :current-page="page.pageNo"
          :page-size="page.pageSize"
          :total="page.total"
          hide-on-single-page
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
          :page-sizes="[10,20,30,40]"
          layout=" total,prev, pager, next,sizes, jumper"
        >
        </el-pagination>
      </div>
    </div>

    <el-dialog :visible.sync="showAddDialog" title="添加" class="addDialog">
      <el-form :model="addSysDepartment" ref="addForm" label-position="left" label-width="80px" :rules="rules">
        <el-form-item
          prop="departName"
          label="部门名称"
        >
          <el-input placeholder="请输入部门名称" v-model="addSysDepartment.departName"></el-input>
        </el-form-item>
        <el-form-item
          prop="pid"
          label="上级部门"
        >
          <el-select v-model="addSysDepartment.pid">
            <el-option
              v-for="item in firstDepartmentList"
              :key="item.id"
              :label="item.departName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

      </el-form>
      <div style="text-align: right">
        <el-button type="danger" size="small" style="border-radius: 0" @click="closeAddDialog">取消</el-button>
        <el-button type="primary" size="small" style="border-radius: 0" @click="confirmAddSysDepartment">确定</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="showEditDialog" title="修改信息" class="editDialog">
      <el-form :model="editSysDepartment" ref="editForm" label-position="left" label-width="80px" :rules="rules">


        <el-form-item
          prop="departName"
          label="部门名称"
        >
          <el-input placeholder="请输入部门名称" v-model="editSysDepartment.departName"></el-input>
        </el-form-item>

        <el-form-item
          prop="pid"
          label="上级部门"
        >
          <el-select v-model="editSysDepartment.pid">
            <el-option
              v-for="item in firstDepartmentList"
              :key="item.id"
              :label="item.departName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item
          prop="status"
          label="状态"
        >
          <el-select v-model="editSysDepartment.status" clearable>
            <el-option
              v-for="item in status"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

      </el-form>
      <div style="text-align: right">
        <el-button type="danger" size="small" style="border-radius: 0" @click="closeEditDialog">取消</el-button>
        <el-button type="primary" size="small" style="border-radius: 0" @click="confirmEditSysDepartment">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import {getAction, postAction} from "@/utils/http";
import {Message} from "element-ui";

export default {
  name: "sysDepartment",
  data() {
    return {
      showAddDialog: false,
      showEditDialog: false,
      addSysDepartment: {
        username: '',
        password: '',
        birthday: ''
      },
      editSysDepartment: {},
      sysDepartmentList: [],
      page: {
        pageNo: 1,
        pageSize: 10,
        total: 0
      },
      rules: {
        departName: [{required: true, message: '请输入部门名称', trigger: 'blur'}],
        pid: [{required: true, message: '请输入上级部门id', trigger: 'blur'}],
      },
      searchParams: {},
      urls: {
        list: "/sysDepartment/sysDepartment/list",
        queryList: "/sysDepartment/sysDepartment/queryList",
        add: "/sysDepartment/sysDepartment/add",
        edit: "/sysDepartment/sysDepartment/edit",
        deleteById: "/sysDepartment/sysDepartment/deleteById",
        deleteBatch: "/sysDepartment/sysDepartment/deleteBatch",
        firstLevel:"/sysDepartment/sysDepartment/listFirstLevel"
      },
      firstDepartmentList:[],
      status: [
        {
          label: '正常',
          value: 1
        },
        {
          label: '冻结',
          value: 2
        }
      ]
    }
  },
  created() {
    this.loadData()
    this.queryFirstLevel()
  },
  methods: {
    reset() {
      this.searchParams = {}
      this.page.total = 0
      this.page.pageSize = 10
      this.page.pageNo = 1
      this.loadData()
    },
    handleCurrentChange(val) {
      this.page.pageNo = val
      this.loadData()
    },
    handleSizeChange(val) {
      this.page.pageSize = val
      this.loadData()
    },
    loadData() {
      let params = {
        ...this.page,
        ...this.searchParams
      }
      getAction(this.urls.list, params).then(res => {
        if (res.success) {
          let {records, current, size, total} = res.result
          this.sysDepartmentList = records
          this.page.pageNo = current
          this.page.pageSize = size
          this.page.total = total
        }
      })
    },
    openAddDialog() {
      this.showAddDialog = true
      this.addSysDepartment = {}
      this.$refs.addForm.resetFields()
    },
    closeAddDialog() {
      this.showAddDialog = false
    },
    openEditDialog(sysDepartment) {
      this.editSysDepartment = Object.assign({}, sysDepartment)
      this.showEditDialog = true
    },
    closeEditDialog() {
      this.showEditDialog = false
      this.$refs.editForm.resetFields()
    },
    confirmAddSysDepartment() {
      console.log(this.addSysDepartment)
      this.$refs.addForm.validate(valid => {
        if (valid) {
          postAction(this.urls.add, this.addSysDepartment).then(res => {
            if (res.success) {
              this.addSysDepartment = {}
              this.showAddDialog = false
              Message.success("添加成功！");
              this.loadData()
            }
          })
        }
      })

    },
    confirmDeleteSysDepartment(id) {
      getAction(this.urls.deleteById, {id}).then(res => {
        if (res.success) {
          Message.success("删除成功！");
          this.loadData()
        }
      })
    },
    confirmEditSysDepartment() {
      this.$refs.editForm.validate(valid => {
        if (valid) {
          postAction(this.urls.edit, this.editSysDepartment).then(res => {
            if (res.success) {
              this.showEditDialog = false
              this.editSysDepartment = {}
              Message.success("修改成功！")
              this.loadData()
            }

          })
        }
      })
    },
    queryFirstLevel(){
      getAction(this.urls.firstLevel).then(res=>{
        if (res.success){
          this.firstDepartmentList = res.result
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
.sysDepartment {
  padding: 20px;

  .dataArea {
    margin-top: 20px;

    .page {
      text-align: center;
      margin-top: 20px;
    }
  }

  .actionArea {
    margin-top: 20px;
  }

  .editDialog {
    width: 1200px;
    margin: 0 auto;
  }

  .addDialog {
    width: 1200px;
    margin: 0 auto;
  }
}
</style>

<style>
.searchArea .el-button {
  border-radius: 0;
}

.actionArea .el-button {
  border-radius: 0;
}

.searchArea .nameInput {
  border-radius: 0;
  margin-right: 20px;
  width: 180px;
}

.editDialog input, .addDialog input {
  border-radius: 0;
}
</style>
